﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
using System.Net;
using log4net;
using System.Configuration;

namespace LogMonitorHost.Tools
{
    public class MailHelper
    {
        /// <summary>
        /// 发送邮件的类
        /// </summary>
        /// <param name="to">接受邮件的地址</param>
        /// <param name="title">发送的标题</param>
        /// <param name="message">发送的信息</param>
        /// <returns></returns>
        public static bool SendEmail(string to, string title, string message)
        {

            try
            {
                string sendEmailUser = System.Configuration.ConfigurationManager.AppSettings["sendEmailUser"].ToString();
                string sendEmailPWD = System.Configuration.ConfigurationManager.AppSettings["sendEmailPWD"].ToString();
                string sendEmailServer = System.Configuration.ConfigurationManager.AppSettings["sendEmailServer"].ToString();
                //string recieveEmailUser = System.Configuration.ConfigurationManager.AppSettings["recieveEmailUser"].ToString();



                MailMessage mail = new MailMessage(sendEmailUser, to);
                //mail.SubjectEncoding = Encoding.UTF8;
                mail.Subject = title;
                mail.IsBodyHtml = true; //是否允许内容为 HTML 格式
                //mail.BodyEncoding = Encoding.UTF8;
                mail.Body = message;

                SmtpClient smtp = new SmtpClient(sendEmailServer);
                //表示以当前登录用户的默认凭据进行身份验证 
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = new NetworkCredential(sendEmailUser, sendEmailPWD); //SMTP 验证
                smtp.Send(mail);

                return true;
            }
            catch
            {
                return false;
            }

        }

    }
}